package com.zhuhjay.service.system.model.domain;

import com.zhuhjay.common.base.util.BeanBuilder;
import com.zhuhjay.common.base.util.BigDecimalUtils;
import com.zhuhjay.service.system.model.entity.SystemFile;
import lombok.Data;

import java.util.Date;

import static com.zhuhjay.common.base.constant.DataConstant.MB;

/**
 * @author ZhuHJay
 * @date 2023/3/29 14:44
 */
@Data
public class FileInfo {

    private String id;
    private String name;
    private double size;
    private String type;
    private String fileParentFolder;
    private String createUser;
    private Date createTime;
    /** 文件路径 */
    private String filePath;

    /**
     * 将 SystemFile 转换为 FileInfo
     *
     * @param file 文件
     * @return FileInfo
     */
    public static FileInfo copyTo(SystemFile file, String username) {
        return BeanBuilder.builder(FileInfo::new)
                .with(FileInfo::setId, file.getId())
                .with(FileInfo::setName, file.getRealName())
                .with(FileInfo::setType, file.getType())
                // 将文件大小单位转换为 -> MB
                .with(FileInfo::setSize, file.getSize() > MB ? BigDecimalUtils.byte2Mb(file.getSize()) : 0)
                .with(FileInfo::setFileParentFolder, file.getDirId())
                .with(FileInfo::setCreateUser, username)
                .with(FileInfo::setCreateTime, file.getCreateTime())
                .build();
    }

}
